/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufscar.compiladores.ast;

/**
 *
 * @author takeshi
 */
public class IfCommand extends Command{
    
    private Expr expr;
    private Command thenPart, elsePart;
    
    public IfCommand(Expr expr, Command thenPart, Command elsePart) {
        this.expr=expr;
        this.thenPart=thenPart;
        this.elsePart=elsePart;
    }

    @Override
    public void genC(PW pw) {
        pw.print("if ( ");
        expr.genC(pw, false);
        pw.out.println(" ) { ");
        if (thenPart != null) {
            pw.add();
            thenPart.genC(pw);
            pw.sub();
            pw.println("}");
        }
        if (elsePart != null) {
            pw.println("else {");
            pw.add();
            elsePart.genC(pw);
            pw.sub();
            pw.println("}");
        }
    }
}
